var {spawnSync} = require('child_process');

var git_exe_path = 'git';//Mac直接为git ； Windows为git.exe路径
var commit_msg = "auto_commit_for_deploy";

function git(code_path,branch_name){
    var error,stdout=[];
    /**/
    // 先查看是否当前分支
    var branch_output_obj = spawnSync(git_exe_path,['branch'],{
        cwd:code_path,
        encoding:'utf8'
    });
    var branch_stdout = (branch_output_obj||{}).stdout;
    var reg = new RegExp("\\* "+branch_name+"\\b");
    if((typeof branch_stdout!=='string')||(!branch_stdout.match(reg))){
        return code_path+'\n配置文件中的分支'+branch_name+'与目录中的实际分支不同，\n请手动切换并确认代码无误后再发布[by git()]';
    } else {
        stdout.push({command:'branch',stdout:branch_stdout});
    }


    //
    var pull_output_obj = spawnSync(git_exe_path,['pull'],{
        cwd:code_path,
        encoding:'utf8'
    });
    error = checkGitOutput(pull_output_obj,'pull');
    if(error){
        return (pull_output_obj.output.join('\n'));
    } else {
        stdout.push({command:'pull',stdout:(pull_output_obj||{}).stdout});
    }


    var add_output = spawnSync(git_exe_path,['add','.'],{
        cwd:code_path,
        encoding:'utf8'
    });

    //
    var commit_output_obj = spawnSync(git_exe_path,['commit','-a','-m',commit_msg],{
        cwd:code_path,
        encoding:'utf8'
    });
    error = checkGitOutput(commit_output_obj,'commit');
    if(error){
        return (commit_output_obj.output.join('\n'));
    } else {
        stdout.push({command:'commit',stdout:(commit_output_obj||{}).stdout});
    }

    //
    var push_output_obj = spawnSync(git_exe_path,['push'],{
        cwd:code_path,
        encoding:'utf8'
    });
    error = checkGitOutput(push_output_obj,'push');
    if(error){
        return error;
    } else {
        stdout.push({command:'push',stdout:(push_output_obj||{}).stdout});
    }

    return {
        success:true,
        stdout:stdout
    };

}

function checkGitOutput(outputObj,step){
    if((!outputObj) || (typeof outputObj!=='object')){
        return step + ':未知错误[输出为空]';
    }
    var status = outputObj.status;
    var stderr = outputObj.stderr;
    if((status===0) || (!stderr)){
        return;
    }
    return stderr||(step + ':未知错误[stderr未知]');
}

//
function pull(code_path,branch_name){
    var error,stdout=[];
    /**/
    // 先查看是否当前分支
    var branch_output_obj = spawnSync(git_exe_path,['branch'],{
        cwd:code_path,
        encoding:'utf8'
    });
    var branch_stdout = (branch_output_obj||{}).stdout;
    var reg = new RegExp("\\* "+branch_name+"\\b");
    if((typeof branch_stdout!=='string')||(!branch_stdout.match(reg))){
        return code_path+'\n配置文件中的分支'+branch_name+'与目录中的实际分支不同，\n请手动切换并确认代码无误后再发布[by pull()]';
    } else {
        stdout.push({command:'branch',stdout:branch_stdout});
    }


    //
    var pull_output_obj = spawnSync(git_exe_path,['pull'],{
        cwd:code_path,
        encoding:'utf8'
    });
    error = checkGitOutput(pull_output_obj,'pull');
    if(error){
        return (pull_output_obj.output.join('\n'));
    } else {
        stdout.push({command:'pull',stdout:(pull_output_obj||{}).stdout});
    }

    return {
        success:true,
        stdout:stdout
    };
}

module.exports = {
    git:git,
    pull:pull
};